from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import tensorflow as tf
mnist = input_data.read_data_sets("./data", one_hot=True)
x = tf.placeholder("float", [None, 784])
y_ = tf.placeholder("float", [None,10])

# W1 = tf.Variable(tf.random_normal(shape=[784,100],stddev=1/np.sqrt(784)))
# b1 = tf.Variable(tf.random_normal(shape=[100],stddev=1/np.sqrt(100)))
W1 = tf.Variable(tf.random_normal(shape=[784,100],stddev=1/np.sqrt(784)))
b1 = tf.Variable(tf.zeros([100]))
# b1 = tf.ones_initializer()

h1 = tf.tanh(tf.matmul(x,W1)+b1)

# W2 = tf.Variable(tf.random_normal(shape=[100,100],stddev=1/np.sqrt(100)))
# b2 = tf.Variable(tf.random_normal(shape=[100],stddev=1/np.sqrt(100)))
#
# h2 = tf.sigmoid(tf.matmul(h1,W2)+b2)

W = tf.Variable(tf.random_normal(shape=[100,10],stddev=1/np.sqrt(10)))
b = tf.Variable(tf.zeros([10]))


y = tf.matmul(h1,W) + b

l2 = tf.contrib.layers.l2_regularizer(5.0/60000)(W)  + tf.contrib.layers.l2_regularizer(5.0/60000)(W1)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)
# loss = 0.1*l1_loss + cross_entropy
loss = cross_entropy+l2
# loss = l1_loss
train_step = tf.train.AdamOptimizer(0.001).minimize(loss)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(0,12):#1000在第12轮次到98%,2000在第5轮达到98%
  for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(540)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
    # temp = sess.run(loss,feed_dict={x: batch_xs, y_: batch_ys})
    # print("step%d cross_entropy: "%(i+1))
    # print(temp)
  correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
  print("第%d准确率："%(i+1))
  print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))